package com.liang.spring.test.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import sun.text.resources.FormatData_sr;

import com.liang.spring.test.dao.TrainInfoMapper;
import com.liang.spring.test.controller.entity.BadbookingInfoExample;
import com.liang.spring.test.controller.entity.TrainInfo;
import com.liang.spring.test.controller.entity.TrainInfoExample;
import com.liang.spring.test.controller.entity.TrainInfoExample.Criteria;
@Controller
@RequestMapping(value = "trainInfo")
public class trainController {

	@Autowired
	private TrainInfoMapper trainInfoMapper;
	
	@RequestMapping(value = "/add")
	public ModelAndView addTrainInfo(Model model, TrainInfo traininfo, HttpServletRequest request){
		try {		
			trainInfoMapper.insert(traininfo);
			model.addAttribute("result", "车次信息维护成功！");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			
		}
		return new ModelAndView("/msg");
	}
	
	@RequestMapping(value = "/update")
	public String updateTrainInfo(TrainInfo traininfo, HttpServletRequest request){
		try {		
			TrainInfoExample example = new TrainInfoExample();
			Criteria criteria = example.createCriteria();
			criteria.andTrainnumberEqualTo(traininfo.getTrainnumber());
			trainInfoMapper.updateByExample(traininfo, example);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			
		}
		return "/updateTrain";
	}
	
	@RequestMapping(value = "/del")
	public String delTrainInfo(String fid, HttpServletRequest request){
		try {		
			TrainInfoExample example = new TrainInfoExample();
			Criteria criteria = example.createCriteria();
			criteria.andTrainnumberEqualTo(fid);
			trainInfoMapper.deleteByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			
		}
		return  "redirect:selAll";		//完成此Controller后调用另外一个Controller
	}
	

	@RequestMapping(value = "/selById")
	public String selById(String fid, Model model){
		try {		
			TrainInfoExample example = new TrainInfoExample();
			Criteria criteria = example.createCriteria();
			criteria.andTrainnumberEqualTo(fid);
			List<TrainInfo> trainList = trainInfoMapper.selectByExample(example);
			model.addAttribute("trainList", trainList);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			
		}
		return "/updateTrain";
	}
	
	@RequestMapping(value = "/selById2")
	public String selById2(String fid, Model model){
		try {		
			TrainInfoExample example = new TrainInfoExample();
			Criteria criteria = example.createCriteria();
			criteria.andTrainnumberEqualTo(fid);
			List<TrainInfo> trainList = trainInfoMapper.selectByExample(example);
			model.addAttribute("trainList", trainList);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			
		}
		return "/buyTicket";
	}
	
	/**
	 * 查询所有车次信息
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/selAll")
	public String selAll(Model model){
		try {		
			TrainInfoExample example = new TrainInfoExample();
			Criteria criteria = example.createCriteria();
			Calendar c = Calendar.getInstance();
			c.setTime(new Date());
			c.add(Calendar.HOUR, 1);
			Date fdate = c.getTime();
//			System.out.println((new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")).format(fdate));
			criteria.andStarttimeGreaterThanOrEqualTo(fdate);
			List<TrainInfo> trainList = trainInfoMapper.selectByExample(example);
			model.addAttribute("trainList", trainList);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			
		}
		return "/seltrain";
	}
	
}
